package tvv.levelup.dao;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.stereotype.Repository;
import tvv.levelup.models.Pet;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

/**
 * Created by VLAD on 02.09.2014.
 */
@Repository
public class PetEquipmentDao implements IPetEquipmentDao {
    private JdbcTemplate jdbcTemplate;

    @Autowired
    public PetEquipmentDao(DriverManagerDataSource dataSource) {
        jdbcTemplate = new JdbcTemplate(dataSource);
    }

    @Override
    public List<Integer> getPetEquipments(Pet p) {
        String query = "SELECT equipment_id FROM petequipment where pet_id = ?";
        List<Integer> resoult = jdbcTemplate.query(query,
                new Object[]{new Integer(p.getId())},
                new RowMapper<Integer>() {
                    @Override
                    public Integer mapRow(ResultSet resultSet, int i) throws SQLException {
                        Integer res = new Integer(resultSet.getInt("equipment_id"));
                        return res;

                    }
                });

        return resoult;

    }

    @Override
    public void savePetEquipment(int pet_id, int eq_id) {
        String query = "INSERT INTO petequipment (pet_id, equipment_id) VALUES (?, ?)";

        jdbcTemplate.update(query, pet_id, eq_id);


    }

    @Override
    public void deletePetEquipment(Pet pet) {

        String query = "DELETE FROM petequipment WHERE pet_id = ?";
        jdbcTemplate.update(query, pet.getId());


    }

}
